package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.SkillService;
import com.wan37.client.service.cache.RoleServiceCache;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.Game;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-07-01 16:14
 **/
@Component(PtoNum.SGAMESKILLRESPONSE + "")
public class SGameSkillResponse implements ReceiveMsg {

    private final static Logger logger = LoggerFactory.getLogger(SGameSkillResponse.class);
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Autowired
    private UserServiceCache userServiceCache;
    @Autowired
    private RoleServiceCache roleServiceCache;

    @Override
    public void execute(Message msg) {
        Game.SGameSkillResponse sGameSkillResponse = (Game.SGameSkillResponse) msg;
        int code = sGameSkillResponse.getCode();
        String content = sGameSkillResponse.getContent();
        switch (code) {
            case Common.GAME_GET_SKILL_SUCCESS:
                sGameSkillResponse.getAllRoleIdToSkillListList().forEach(allRoleIdToSkillList -> {
                    if (roleServiceCache.getRoleInMap().containsKey(allRoleIdToSkillList.getAllRoleId())) {
                        Map<String, GameEntity.Skill> skillMap = new HashMap<>();
                        allRoleIdToSkillList.getSkillList().forEach(skill -> {
                            skillMap.put(skill.getId(), skill);
                        });
                        roleServiceCache.getRoleInMap().get(allRoleIdToSkillList.getAllRoleId()).setSkillMap(skillMap);
                    } else {
                        Map<String, GameEntity.Skill> skillMap = new HashMap<>();
                        allRoleIdToSkillList.getSkillList().forEach(skill -> {
                            skillMap.put(skill.getId(), skill);
                        });
                        SkillService.allRoleIdToSkillMap.put(allRoleIdToSkillList.getAllRoleId(),
                                skillMap);
                    }
                    //初始化实体可以释放的技能队列
                    SkillService.addAllRoleIdToPBQMap(allRoleIdToSkillList.getAllRoleId());
                    for (GameEntity.Skill skill : allRoleIdToSkillList.getSkillList()) {
                        logger.info("实体 " + allRoleIdToSkillList.getAllRoleId() + " 获取技能：" + skill.getId() + "-----" + skill.getName());
                    }
                });

                break;
            default:
                break;
        }
    }
}
